package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.UserMap;
import com.example.demo.po.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    UserMap userMap;

    @Autowired
    UserService userService;

    @PostMapping("/index")
    @ResponseBody
    public String hello(@RequestBody User requestUser){

        return userMap.getUser(requestUser.getId()).toString();
    }


    @PostMapping("/regisyry")
    @ResponseBody
    public Result regisyry(@RequestBody User requestUser){

       if (requestUser==null) {
           return ResultUtil.fail("参数有误");
       }


        return userService.RegistryUser(requestUser);
    }


    @GetMapping("/login/{username}/{password}")
    @ResponseBody
    public Result LoginUser(@PathVariable("username") String username, @PathVariable("password") String password,HttpSession session){
        User user =   userService.getUserByLogin(username,password);

        if(user == null){
            return ResultUtil.fail("用户名或密码出错..");
        }
        session.setAttribute("user",user);

    return ResultUtil.success(user);
    }

    @GetMapping("/getUser")
    @ResponseBody
    public Result GetUser(HttpServletRequest request){


        return ResultUtil.success(request.getSession().getAttribute("user"));
    }




}
